package com.qf.service.impl;/*
 *徐韬
 *2021/11/5
 *14:22
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.cache.acountCache;
import com.qf.constons.resultCode;
import com.qf.events.updateAcountEvents;
import com.qf.exceptions.addDataException;
import com.qf.exceptions.queryDataException;
import com.qf.exceptions.updateException;
import com.qf.mapper.acountRecordMapper;
import com.qf.pojo.MyAuthUser;
import com.qf.pojo.TAcountRecord;
import com.qf.service.acountService;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import springfox.documentation.spring.web.readers.operation.CachingOperationNameGenerator;

import java.util.List;

@Service
@Transactional
public class acountServiceimpl implements acountService {
    private acountCache acountCache;
    private acountRecordMapper acountRecordMapper;
    private ApplicationContext context;
@Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    @Autowired
    public void setAcountRecordMapper(com.qf.mapper.acountRecordMapper acountRecordMapper) {
        this.acountRecordMapper = acountRecordMapper;
    }

    @Autowired
    public void setAcountCache(com.qf.cache.acountCache acountCache) {
        this.acountCache = acountCache;
    }

    @Override
    public PageInfo<TAcountRecord> showAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        MyAuthUser myAuthUser =(MyAuthUser) principal;
        Long type = myAuthUser.getType();
        if(type!=1){
            throw new queryDataException("你没有权限查看该数据", resultCode.NO_ROOT);
        }
        List<TAcountRecord> tAcountRecords = acountCache.getfinlAll();
        PageInfo<TAcountRecord> pageInfo = new PageInfo<>(tAcountRecords);
        return pageInfo;


    }

    @Override
    public void addTacont(TAcountRecord tAcountRecord) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        MyAuthUser myAuthUser = (MyAuthUser) principal;
        Long type = myAuthUser.getType();
        if(type !=1){
            throw new addDataException("您没权限添加数据",resultCode.NO_ROOT);
        }
        acountRecordMapper.insert(tAcountRecord);
        context.publishEvent(new updateAcountEvents());
    }

    @Override
    public void update(TAcountRecord tAcountRecord) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        MyAuthUser myAuthUser = (MyAuthUser) principal;
        Long type = myAuthUser.getType();
        if(type!=1){
            throw new updateException("你没有权限修改该条数据",resultCode.NO_ROOT);
        }
        acountRecordMapper.update(tAcountRecord);
        context.publishEvent(new updateAcountEvents());
    }

    @Override
    public void deletedByIds(List<Integer> ids) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        MyAuthUser myAuthUser = (MyAuthUser) principal;
        Long type = myAuthUser.getType();
        if(type!=1){
            throw new updateException("您没有权限删除该条数据",resultCode.NO_ROOT);
        }
        acountRecordMapper.deleteByIds(ids);
        context.publishEvent(new updateAcountEvents());
    }
}
